package com.xiaodimall.backsys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaodimall.backsys.common.JsonResult;
import com.xiaodimall.backsys.mapper.GoodsCategoryMapper;
import com.xiaodimall.backsys.pojo.GoodsCategory;
import com.xiaodimall.backsys.service.GoodsCategoryService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:
 * @Date: Created in 2022/11/30 18:50
 * @Description:
 */
@RestController
@RequestMapping("/admin/category")
public class AdminGoodsCategoryController {
    @Resource
    GoodsCategoryService goodsCategoryService;

    @Resource
    GoodsCategoryMapper goodsCategoryMapper;

    @GetMapping("/{goodsCategoryLevel}")
    public JsonResult getGoodsCategoryByLevel(@PathVariable("goodsCategoryLevel") Integer goodsCategoryLevel){
        return goodsCategoryService.getGoodsCategoryByLevel(goodsCategoryLevel);
    }

    @GetMapping("/parent/{parentId}")
    public List<GoodsCategory> getGoodsCategoryByParentId(@PathVariable("parentId") Integer parentId){
        QueryWrapper<GoodsCategory> goodsCategoryQueryWrapper = new QueryWrapper<>();
        goodsCategoryQueryWrapper.eq("parent_id",parentId);
        return goodsCategoryMapper.selectList(goodsCategoryQueryWrapper);
    }

    @PostMapping()
    public JsonResult addGoodsCategory(GoodsCategory goodsCategory){
        return goodsCategoryService.addGoodsCategory(goodsCategory);
    }

    @DeleteMapping("/id")
    public JsonResult deleteGoodsCategoryById(Integer categoryId){
        return goodsCategoryService.deleteGoodsCategoryById(categoryId);
    }

    @PutMapping()
    public JsonResult updateById(Integer categoryId, String categoryName, Integer categoryRank){
        System.out.println(categoryId);
        System.out.println(categoryName);
        System.out.println(categoryRank);
        return goodsCategoryService.updateById(categoryId,categoryName,categoryRank);
    }
}
